<template>
	<view class="container">
		<web-view :src="'http://shop.coins32.cn/?time=' + currentTime" @message="message"></web-view>
		<!-- <web-view src="http://192.168.0.112:8080/" @message="message" ref="webview"></web-view> -->
	</view>
</template>

<script>
import permision from "@/common/permission.js"
export default {
	data() {
		return {
			currentTime: Math.random().toString().slice(2)
		};
	},
	methods: {
		async message(event) {
			var data = event.detail.data && event.detail.data[0];
			if (data && data.action == 'saveImage') {
				uni.downloadFile({
					url: data.url,
					success: res => {
						if (res.statusCode === 200) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: function() {
									uni.showToast({
										title: '保存成功',
										icon: 'none'
									});
								},
								fail: function() {
									uni.showToast({
										title: '保存失败',
										icon: 'none'
									});
								}
							});
						}
					}
				});
			}
			if (data && data.action == 'qrScan') {
				console.log(data);
				await this.scan()
			}
		},
		async checkPermission(code) {
			let status = permision.isIOS ? await permision.requestIOS('camera') :
				await permision.requestAndroid('android.permission.CAMERA');
			if (status === null || status === 1) {
				status = 1;
			} else {
				uni.showModal({
					content: "需要相机权限",
					confirmText: "设置",
					success: function(res) {
						if (res.confirm) {
							permision.gotoAppSetting();
						}
					}
				})
			}
			return status;
		},
		async scan(callback) {
			var vm = this;
			// #ifdef APP-PLUS
			let status = await this.checkPermission();
			if (status !== 1) {
				return;
			}
			// #endif
			uni.scanCode({
				success: (res) => {
					vm.wb.evalJS('window.handlerResult(' + JSON.stringify(res)+ ')')
				},
				fail: (err) => {
				}
			});
		}
	},
	onBackPress(e) {
		console.log(this.wb.getTitle());
		if (this.wb.getTitle() != '首页') {
			this.wb.back();
			return true;
		}
	},
	onPullDownRefresh: function() {
		console.log(1);
	},
	onLoad() {
		// #ifdef APP-PLUS
		var webview = this.$mp.page.$getAppWebview();
		var sysinfo = uni.getSystemInfoSync();
		var vm = this;
		function setWindowHeight() {
			var wb = webview.children()[0];
			if (wb) {
				vm.wb = wb;
				vm.wb.setStyle({ top: sysinfo.safeArea.top, height: sysinfo.safeArea.height });
			} else {
				setTimeout(setWindowHeight, 100);
			}
		}
		setWindowHeight();
		// #endif
	}
};
</script>

<style></style>
